#include "login.h"
#include "ui_login.h"

LogIn::LogIn(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LogIn)
{

    ui->setupUi(this);
    this->setWindowTitle("OpenChatRoom");
    this->setFixedSize(850,500);
    //设置透明度

    QPalette PAllbackground = this->palette();
    QImage ImgAllbackground(":/source/login.jpg");
    QImage fitimgpic=ImgAllbackground.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
    PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
    this->setPalette(PAllbackground);


//    db = QSqlDatabase::addDatabase("QMYSQL");
//    db.setHostName("47.115.215.143");
//    db.setPort(3307);
//    db.setDatabaseName("YUNMYSQL");//数据库名字
//    db.setUserName("root");//账号
//    db.setPassword("123456");//密码
//    db.open();


    QSqlQuery query;
    query.exec("select * from Remember order by time desc limit 5;");
    QStringList mysqlMonth; //用于保存遍历到的账号，这个类型类似于数组和集合
    while(query.next())
    {
        //将所有账户进行保存
        mysqlMonth<<query.value(0).toString();
    }
    ui->uidComboBox->addItems(mysqlMonth); //将遍历的账号写入下拉列表

    //自动登录功能
    QSettings fg("saveflag.ini",QSettings::IniFormat);
    AutoLogin= fg.value("isauto").toString();
    RemeberPasswd= fg.value("ispassword").toString();
    if(RemeberPasswd== "true")
    {
        ui->remmberpassword->setChecked(true);
    }
    if(AutoLogin== "true")
    {
        ui->autocheckBox->setChecked(true);
    }


    //password输入后回车登录
    //connect(ui->password,SIGNAL(returnPressed()),this,SLOT(on_login_clicked()));

    //返回按钮
    connect(ui->back,&QPushButton::clicked,this,[=](){
        Widget* wid=new Widget;
        this->hide();
        wid->show();
    });


    //忘记密码----跳转到忘记密码界面
    this->forgetpassword=new ForgetPassword();
    connect(ui->forget,&QPushButton::clicked,this,[=](){
        this->hide();
        this->forgetpassword->show();//想要登录后释放之前的界面？
    });

    //从忘记密码界面返回
    connect(this->forgetpassword,&ForgetPassword::back,this,[=](){
        this->forgetpassword->hide();
        this->show();
    });



}

LogIn::~LogIn()
{
    delete ui;
    //db.close();
}

void LogIn::logInit()
{

    QSqlQuery query;
    //遍历账户
    query.exec("select * from Remember order by time desc limit 5");
    //保存已保存密码的账号 最多5个账户
    QString account_qstr[5];
    //用于数组
    int i = 0;
    while(query.next())
    {
        //保存遍历值
        account_qstr[i++] = query.value(1).toString();
    }

    if(ui->uidComboBox->currentIndex()==0 )
    {
        ui->password->setText(account_qstr[0]);

    }
    if(ui->uidComboBox->currentIndex()==1  )
    {
        ui->password->setText(account_qstr[1]);

    }
    if(ui->uidComboBox->currentIndex()==2 )
    {
        ui->password->setText(account_qstr[2]);
    }
    if(ui->uidComboBox->currentIndex()==3  )
    {
        ui->password->setText(account_qstr[3]);
    }
    if(ui->uidComboBox->currentIndex()==4 )
    {
        ui->password->setText(account_qstr[4]);
    }



}

//登录按钮
void LogIn::on_login_clicked()
{

        savecflag();

        QString uid=ui->uidComboBox->currentText();
        QString password=ui->password->text();

        QString sql=QString("select Uid,Password from User where Uid='%1' and Password='%2'").arg(uid).arg(password);
        QSqlQuery query;
        if(query.exec(sql)&&query.next())
        {

                if(ispassword)
                {
                    QDateTime current_date_time =QDateTime::currentDateTime();
                    QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm:ss");
                    qDebug()<<current_date;

                    //这里需要先判断Remember表里是否存在
                    sql=QString("select Uid from Remember where Uid='%1'").arg(uid);
                    query.exec(sql);
                    if(query.next()&&QString::compare(uid,query.value(0).toString())==0)
                    {
                        sql=QString("update Remember set password='%1',time='%2' where uid='%3'").arg(password).arg(current_date).arg(uid);

                        if(query.exec(sql)) {
                            qDebug() << "修改成功";
                        } else {
                            qDebug() << "修改失败：" ;
                        }
                    }
                    else
                    {
                        sql=QString("insert into Remember(Uid,Password,time) values('%1','%2','%3')").arg(uid).arg(password).arg(current_date);

                        if(query.exec(sql)) {
                            qDebug() << "插入成功";
                        } else {
                            qDebug() << "插入失败：" ;
                        }
                    }
                }


                sql=QString("select Name from User where Uid='%1'").arg(uid);
                QString name;
                if(query.exec(sql)&&query.next())
                {
                    name=query.value(0).toString();
                }
                Chat *chat=new Chat(nullptr,uid,name);

                this->hide();
                chat->show();
           }

}

void LogIn::on_remmberpassword_stateChanged(int)
{

    if(ui->remmberpassword->isChecked())
    {
        ispassword= true;
    }
    else
    {
        ispassword= false;
    }


}

void LogIn::on_uidComboBox_currentIndexChanged(int)
{
    logInit();
}


void LogIn::on_autocheckBox_stateChanged(int)
{
    if(ui->autocheckBox->isChecked())
    {
        isauto=true;
    }
    else
    {
        isauto=false;
    }

}

void LogIn::savecflag()
{
    QSettings fg("saveflag.ini",QSettings::IniFormat);
    fg.setValue("isauto",isauto);
    fg.setValue("ispassword",ispassword);
    fg.sync();
}
